package org.dolphin.entity.promo;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;
import org.dolphin.base.constant.DatabaseAttribute;
import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.util.ObjectUtils;

@Entity
@Table(name = "MS_VOUCHER", uniqueConstraints = @UniqueConstraint(columnNames = {"VOUCHER_CODE",
    "VERIFICATION_CODE"}, name = "UQ_MS_VOUCHER_VOUCHER_CODE_VERIFICATION_CODE"))
public class Voucher implements Serializable {
  private static final long serialVersionUID = -3493147892118421639L;
  private String id;
  private String voucherCode;
  private String verificationCode;
  private Date startDate;
  private Date endDate;
  private BigDecimal amount;
  private Boolean active;
  private VoucherBatch voucherBatch;

  @Override
  public boolean equals(Object obj) {
    if (obj == this) {
      return true;
    } else if (obj instanceof Voucher) {
      Voucher oth = (Voucher) obj;
      return ObjectUtils.nullSafeEquals(getId(), oth.getId());
    }
    return false;
  }

  @Column(name = "ACTIVE", nullable = false)
  public Boolean getActive() {
    return active;
  }

  @Column(name = "AMOUNT", nullable = false)
  public BigDecimal getAmount() {
    return amount;
  }

  @Temporal(TemporalType.DATE)
  @Column(name = "END_DATE", nullable = true)
  public Date getEndDate() {
    return endDate;
  }

  @Id
  @GeneratedValue(generator = "uuid2")
  @GenericGenerator(name = "uuid2", strategy = "org.dolphin.base.id.generator.CustomUUIDGenerator")
  @Column(name = "ID", nullable = false, length = DatabaseAttribute.ID_LENGTH)
  public String getId() {
    return id;
  }

  @Temporal(TemporalType.DATE)
  @Column(name = "START_DATE", nullable = true)
  public Date getStartDate() {
    return startDate;
  }

  @Column(name = "VERIFICATION_CODE", nullable = false, length = 50)
  public String getVerificationCode() {
    return verificationCode;
  }

  @ManyToOne(fetch = FetchType.LAZY, optional = false)
  @JoinColumn(name = "VOUCHER_BATCH_ID", nullable = false)
  @ForeignKey(name = "FK_MS_VOUCHER_VOUCHER_BATCH_ID")
  public VoucherBatch getVoucherBatch() {
    return voucherBatch;
  }

  @Column(name = "VOUCHER_CODE", nullable = false, length = 50)
  public String getVoucherCode() {
    return voucherCode;
  }

  @Override
  public int hashCode() {
    int hash = ObjectUtils.nullSafeHashCode(getId());
    return hash;
  }

  public void setActive(Boolean active) {
    this.active = active;
  }

  public void setAmount(BigDecimal amount) {
    this.amount = amount;
  }

  public void setEndDate(Date endDate) {
    this.endDate = endDate;
  }

  public void setId(String id) {
    this.id = id;
  }

  public void setStartDate(Date startDate) {
    this.startDate = startDate;
  }

  public void setVerificationCode(String verificationCode) {
    this.verificationCode = verificationCode;
  }

  public void setVoucherBatch(VoucherBatch voucherBatch) {
    this.voucherBatch = voucherBatch;
  }

  public void setVoucherCode(String voucherCode) {
    this.voucherCode = voucherCode;
  }
}
